import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';

export default {
  name: 'login',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      openId: '',
      userName: '',
      userPass: '',
      idCardNeeds: false,
      placeholderText: '请输入学员姓名'
    }
  },
  mounted () {
    var _this = this
    _this.openId = window.sessionStorage.getItem('openId')
    console.log('openid', _this.openId)
    _this.siteId = window.sessionStorage.getItem('siteId')
    console.log(_this.siteId)
    console.log(CONSTANT.SETTING)

    http.get(CONSTANT.SETTING + '?siteId=' + _this.siteId + '&', {}, (res) => {
      //console.log(res)
      _this.idCardNeeds = res.idCardNeeds
      //if (_this.idCardNeeds == true) {
      //_this.placeholderText = '请输入学员身份证'
     // }
    })
  },
  methods: {
    goHello () {
      var _this = this
      var tel = /^[1][3,4,5,7,8][0-9]{9}$/
      var idcard = /^(\d{18}|\d{17}(x|X))$/
      if (_this.userPass == '') {
        alert('请输入手机号码！')
      } else if (!tel.test(_this.userPass)) {
        alert('请输入正确的手机号码！')
      } else if (_this.userName == '') {
        alert(_this.placeholderText)
      }
      //else if (!idcard.test(_this.userName) && _this.idCardNeeds == true) {
      //  alert('请输入正确的身份证号码！')
     // }
      else {
        http.post(CONSTANT.LOGIN, {
          userName: _this.userName,
          userPass: _this.userPass,
          siteId: _this.siteId,
          openId: _this.openId
        }, (res) => {
          console.log(res)
          _this.studentId = res.id
          window.sessionStorage.setItem('studentId', res.id)
          window.sessionStorage.setItem('studentName', res.name)
          if (res.msg) {
            alert(res.msg)
          } else {
            this.$router.push({path: '/tabbar'})
          }
        })
      }
    },
    goBack () {
      window.history.go(-1)
    }
  }
}
